(function(K) {
	
var Assert = YAHOO.util.Assert;
var ArrayAssert = YAHOO.util.ArrayAssert;

function getDependencies(callback) {
    K.classLoader().
    requires("tk.k90tools.lang.Array").
    load(function(I) { callback(I); });
}
    
K.classLoader(K.data("testConfig").basePath).
provides("tk.k90tools.lang.ArrayTest").
load(function(I) {
	
    return function() {
        var test = new YAHOO.tool.TestCase({
            name : "tk.k90tools.lang.ArrayTest",
            
            testAsArray : function() {
	        	getDependencies(function(I) {
	        		test.resume(function() {
		                var a = [1,2,3];
		                var b = I.Array.asArray(a);
		                ArrayAssert.itemsAreSame(a,b);
		                Assert.isArray(b);
	        		});
	        	});
	        	this.wait();
	        	
            },
            
            testFilter : function() {
	        	getDependencies(function(I) {
	        		test.resume(function() {
		            	var a = [1,3,5,7,9];
		            	var b = [5,7,9];
		            	
		            	var filterFunction = function(val) {
		            		return val >= 5;
		            	};
		            	
		            	var c = I.Array.filter(a, filterFunction);
		            	ArrayAssert.itemsAreEqual(b,c);
	        		});
	        	});
	        	this.wait();
            },
                
            testForEach : function() {
	        	getDependencies(function(I) {
	        		test.resume(function() {
		        		var a = [1,2,3];
		        		var b = [];
		        		var i = 0;
		        		var o = {};
		        		I.Array.forEach(
		        			a, 
		        			function(item, index, array) {
		        				Assert.areSame(array, a);
		        				Assert.areSame(this, o);
		        				Assert.areSame(index, i);
		        				b.push(item);
		        				i++;
		        			}, 
		        			o
		        		);
	        		});
	        	});
	        	this.wait();
            },
            

            testEvery : function() {
	        	getDependencies(function(I) {
	        		test.resume(function() {
		        		var a = [1,2,3];
		        		var o = {};
		        		var i = 0;
		        		var result = I.Array.every(
		        			a, 
		        			function(item, index, array) {
		        				Assert.areSame(array, a);
		        				Assert.areSame(this, o);
		        				Assert.areSame(index, i);
		        				i++;
		        				return true;
		        			}, 
		        			o
		        		);
		        		Assert.areSame(i, 3);
		        		Assert.isTrue(result);
		        		
		        		var i = 0;
		        		var result = I.Array.every(
		        			a, 
		        			function(item, index, array) {
		        				Assert.areSame(array, a);
		        				Assert.areSame(this, o);
		        				Assert.areSame(index, i);
		        				i++;
		        				return false;
		        			}, 
		        			o
		        		);
		        		Assert.areSame(i, 1);
		        		Assert.isFalse(result);
	        		});
	        	});
	        	this.wait();
            },
            
            testMap : function() {
	        	getDependencies(function(I) {
	        		test.resume(function() {
		        		var a = [1,2,3];
		        		var b = [2,4,6];
		        		var i = 0;
		        		var o = {};
		        		var c = I.Array.map(
		        			a, 
		        			function(item, index, array) {
		        				Assert.areSame(array, a);
		        				Assert.areSame(this, o);
		        				Assert.areSame(index, i);
		        				i++;
		        				
		        				return item * 2;
		        			}, 
		        			o
		        		);
		        		
		            	ArrayAssert.itemsAreEqual(b,c);
	        		});
	        	});
	        	this.wait();
            },
            
            testSome : function() {
	        	getDependencies(function(I) {
	        		test.resume(function() {
		        		var a = [1,2,3];
		        		var o = {};
		        		var i = 0;
		        		var result = I.Array.some(
		        			a, 
		        			function(item, index, array) {
		        				Assert.areSame(array, a);
		        				Assert.areSame(this, o);
		        				Assert.areSame(index, i);
		        				i++;
		        				return false;
		        			}, 
		        			o
		        		);
		        		Assert.areSame(i, 3);
		        		Assert.isFalse(result);
		        		
		        		i = 0;
		        		var result = I.Array.some(
		        			a, 
		        			function(item, index, array) {
		        				Assert.areSame(array, a);
		        				Assert.areSame(this, o);
		        				Assert.areSame(index, i);
		        				i++;
		        				return true;
		        			}, 
		        			o
		        		);
		        		Assert.areSame(i, 1);
		        		Assert.isTrue(result);
	        		});
	        	});
	        	this.wait();
            },
            
            testReduce : function() {
	        	getDependencies(function(I) {
	        		test.resume(function() {
		        		var a = [0,1,2,3,4];
		        		
		        		var calls = [
							{
								previousValue : 10,
								currentValue : 0,
								index: 0
							},
							{
								previousValue : 10,
								currentValue : 1,
								index: 1
							},
							{
								previousValue : 11,
								currentValue : 2,
								index: 2
							},
							{
								previousValue : 13,
								currentValue : 3,
								index: 3
							},
							{
								previousValue : 16,
								currentValue : 4,
								index: 4
							}
						];
		        		
		        		var callIndex = 0;
		        		
		        		var reduceFunction = function(previousValue, currentValue, index, array) {
		        			var values = calls[callIndex++];
		        			
		        			Assert.areSame(values.previousValue, previousValue);
		        			Assert.areSame(values.currentValue, currentValue);
		        			Assert.areSame(values.index, index);
		        			
		        			return previousValue + currentValue;
		        		};
		        		
		        		var returnValue = I.Array.reduce(a, reduceFunction, 10);
		        		
		        		Assert.areSame(20, returnValue);
	        		});
	        	});
	        	this.wait();
            },
            
            testReduceRight : function() {
	        	getDependencies(function(I) {
	        		test.resume(function () {
		        		var a = [0,1,2,3,4];
		        		
		        		var calls = [
							{
								previousValue : 20,
								currentValue : 0,
								index: 0
							},
							{
								previousValue : 19,
								currentValue : 1,
								index: 1
							},
							{
								previousValue : 17,
								currentValue : 2,
								index: 2
							},
							{
								previousValue : 14,
								currentValue : 3,
								index: 3
							},
							{
								previousValue : 10,
								currentValue : 4,
								index: 4
							}
						];
		        		
		        		var callIndex = 4;
		        		
		        		var reduceFunction = function(previousValue, currentValue, index, array) {
		        			var values = calls[callIndex--];
		        			
		        			Assert.areSame(values.previousValue, previousValue);
		        			Assert.areSame(values.currentValue, currentValue);
		        			Assert.areSame(values.index, index);
		        			
		        			return previousValue + currentValue;
		        		};
		        		
		        		var returnValue = I.Array.reduceRight(a, reduceFunction, 10);
		        		
		        		Assert.areSame(20, returnValue);
	        		});
	        	});
	        	this.wait();
            },
            
            testIndexOf : function() {
            	getDependencies(function(I) {
            		test.resume(function () {
                    	var a = [0,1,2,3,4,0,1,2,3,4];
                    	var i = I.Array.indexOf(a, 3);
                    	Assert.areSame(3, i);
                    	
                    	i = I.Array.indexOf(a, 3, 4);
                    	Assert.areSame(8, i);
                    	
                    	i= I.Array.indexOf(a, "3");
                    	Assert.areSame(-1, i);
            		});
            	});
            	
            	this.wait();
            },
            
            testLastIndexOf : function() {
            	getDependencies(function(I) {
            		test.resume(function () {
                    	var a = [0,1,2,3,4,0,1,2,3,4];
                    	var i = I.Array.lastIndexOf(a, 3);
                    	Assert.areSame(8, i);
                    	
                    	i = I.Array.lastIndexOf(a, 3, 4);
                    	Assert.areSame(3, i);
                    	
                    	i = I.Array.lastIndexOf(a, "3");
                    	Assert.areSame(-1, i);
            		});
            	});
            	
            	this.wait();
            }
        });
        
        return test;
    };
});

})(k90tools);